package com.kkwrite.regimen.provider.controller.shop;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kkwrite.regimen.common.constant.Constant;
import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.shopItem.InsertShopItemInDTO;
import com.kkwrite.regimen.common.entity.dto.shopItem.InsertShopItemOutDTO;
import com.kkwrite.regimen.provider.domain.RegShopDO;
import com.kkwrite.regimen.provider.exception.ServiceException;
import com.kkwrite.regimen.provider.repository.RegShopRepository;
import com.kkwrite.regimen.provider.service.shop.ShopItemService;

import java.util.Optional;

/**
 * 项目店铺
 *
 * @author jinying
 */
@RestController
@RequestMapping("/shopItem")
public class ShopItemController {

@Resource(name = "shopItemService")
private ShopItemService shopItemService;

@Resource(name = "regShopRepository")
private RegShopRepository regShopRepository;

    @GetMapping("/getShopByItem")
    public OutDTO getShopByItem(@RequestParam Long itemId, @RequestParam String shopKeywords,
                                                   double lat, double lng,String city) {
        try {
            return OutDTO.of(Constant.BOOLEAN_STRING_TRUE, "OK", "OK",
                    shopItemService.queryShopByItem(itemId, shopKeywords, lat, lng,city));
        } catch (ServiceException e) {
            return OutDTO.of(Constant.BOOLEAN_STRING_FALSE, "服务异常！异常信息：" + e.getMessage());
        }
    }
    
    @PostMapping("/insertShopItem")
    public OutDTO insertShop(@RequestBody InsertShopItemInDTO inDto,HttpServletRequest request){
    	if (inDto.getShopId() == null || inDto.getItems().length == 0) {
    		return OutDTO.of("faild", "店铺id和项目id为必须参数");
    	}

		RegShopDO regShopDOParam = new RegShopDO();
		regShopDOParam.setShopId(inDto.getShopId());
		Optional<RegShopDO> regShopDOResult = regShopRepository.findOne(Example.of(regShopDOParam));
    	RegShopDO regShopDO = regShopDOResult.get();
    	if (regShopDO == null) {
    		return OutDTO.of("faild", "请检查项目id是否正确");
    	}

		InsertShopItemOutDTO insertShopItemOutDTO =  shopItemService.insertShopItem(inDto);
		OutDTO outDto = new OutDTO();
		outDto.setSuccess(insertShopItemOutDTO.getSuccess());
		outDto.setMsg(insertShopItemOutDTO.getMsg());
    	return outDto;
    }

}
